package fx.com.controller;


import fx.com.FxDemoApplication;
import fx.com.entity.User;
import fx.com.service.impl.UserServiceImpl;
import javafx.animation.FadeTransition;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.util.Duration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.List;


/**
 * @author:yanjizhw@163.com
 * @date:2019/1/17 8:44
 * 描述:
 * @since:1.0.0
 */
@Component
public class LoginController {

    @Autowired
    private UserServiceImpl userService;

    @FXML
    private Button button;

    @FXML
    private TextField textField;

    @FXML
    private PasswordField passwordField;

    @FXML
    private AnchorPane anchorPane;

    private Stage stage;

    public List<User> onclick() throws IOException {

        stage = (Stage) button.getScene().getWindow();

        String username = null;
        String password = null;

        if (textField != null && passwordField != null){
            username = textField.getText();
            password = passwordField.getText();
        }
            List<User> lists = userService.login(username, password);

        if (lists.size() > 0 ){

            AnchorPane root = FXMLLoader.load(getClass().getResource("/main.fxml"));

            Scene scene = new Scene(root, 800, 700);

            Stage s = new Stage();
            s.setTitle("主页面");
            s.setScene(scene);
            s.setResizable(false);
            s.show();

            stage.close();

        }else {

            FadeTransition fade = new FadeTransition();
            fade.setDuration(Duration.seconds(0.1));
            fade.setNode(anchorPane);
            fade.setFromValue(0);
            fade.setToValue(1);
            fade.play();
        }

        return lists;
}

    public void initialize(){

    }

}
